Skip to content

Tidy the investment graph module - #1576

Merged
tsmbland merged 6 commits into
mainfrom
investment_graph_tidy
Nov 9, 2026
Merged

tsmbland merged 6 commits into
mainfrom
investment_graph_tidy

Conversation

@tsmbland

@tsmbland tsmbland commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Tidying up the investment graph module in preparation for #1557 and future work. Heavily aided by AI. My initial attempt at #1557 was a bit clunky, so I asked for ideas to tidy up the module, make these refactoring changes on the base branch, then rebase #1557 on top, therefore separating refactoring from the feature changes. I also told it some ideas about future changes, where I expect we'll switch the investment graph to have agents as nodes rather than market sets (just a vague idea at this point), and to keep this in the back of its mind.

Main changes:

  • order_sccs has become order_scc - ordering one SCC rather than all of them, called in a loop. I think this makes the function a bit cleaner and removes a nesting level.
  • removes init_investment_graph entirely. Graphs are now initialised with a single filter_map call. The current approach filter_maps, then copies nodes and edges to a new graph, which I think is a leftover from a time where at this point we were combining multiple region-level graphs, but no longer have to do this after Combine region graphs into single graph for each year #1569.
  • We're also now deferring the point at which we convert (commodity, region) nodes to MarketSet until after cycle detection and ordering. Previously we made the switch before, which meant that we had to pass two different graphs to order_sccs (one with (commodity, region) nodes and one with MarketSet nodes). This is way cleaner now.
  • Helper functions is_market and to_market_set
  • Some helper functions in the tests for building test graphs.

I think this is undoubtedly an improvement.

Fixes # (issue)

Type of change

  • Bug fix (non-breaking change to fix an issue)
  • New feature (non-breaking change to add functionality)
  • Refactoring (non-breaking, non-functional change to improve maintainability)
  • Optimization (non-breaking change to speed up the code)
  • Breaking change (whatever its nature)
  • Documentation (improve or add documentation)

Key checklist

  • All tests pass: $ cargo test
  • The documentation builds and looks OK: $ cargo doc
  • Update release notes for the latest release if this PR adds a new feature or fixes a bug
    present in the previous release

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.62304% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.89%. Comparing base (60a012a) to head (cb2826f).

Files with missing lines Patch % Lines
src/graph/investment.rs 91.62% 6 Missing and 9 partials ⚠️
src/input.rs 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1576      +/-   ##
==========================================
- Coverage   90.90%   90.89%   -0.02%     
==========================================
  Files          61       61              
  Lines        9116     9138      +22     
  Branches     9116     9138      +22     
==========================================
+ Hits         8287     8306      +19     
+ Misses        507      505       -2     
- Partials      322      327       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the investment graph refactor.

No open issues 🎉

What changed in this PR

This PR simplifies the investment graph module, retaining the existing approach to ordering markets and grouping independent investment decisions.

Changes:

  • Refactors graph filtering and cycle condensation to use commodity graph indices directly.
  • Replaces order_sccs with a per-component order_scc helper and simplifies test setup and assertions.
  • Updates documentation and a pricing comment to reference the renamed helper.
File Description
src/​simulation/​prices.rs Updates the helper name in a comment; no functional change.
src/​graph/​investment.rs Simplifies investment graph construction, cycle ordering, and test helpers.
docs/​model/​commodity_graphs.md Updates the cycle-ordering API reference and link.

Review effort: Balanced

Base automatically changed from graph_docs to main November 6, 2026 12:47
@tsmbland
tsmbland force-pushed the investment_graph_tidy branch from 6cc3e82 to c0efc7a Compare November 6, 2026 13:42
@tsmbland
tsmbland marked this pull request as ready for review November 6, 2026 13:43
@tsmbland
tsmbland requested a review from alexdewar November 6, 2026 13:44

@alexdewar alexdewar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Always nice to have a PR that removes code.

Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs
///
/// Components with one market become `MarketSet::Single`, and those with multiple markets become
/// `MarketSet::Cycle`.
fn to_market_set(graph: &CommoditiesGraph, scc: &[NodeIndex]) -> MarketSet {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could define a from_scc method for MarketSet instead. Up to you.

Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Outdated
) {
/// `scc` holds the commodity graph indices of the SCC's markets, and is sorted in place into the
/// final order. If the MILP cannot be solved, a warning is logged and the order is left unchanged.
fn order_scc(graph: &CommoditiesGraph, commodities: &CommodityMap, scc: &mut [NodeIndex]) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that there are a lot of Vecs of size n in this function, whereas you could have a smaller number (maybe just one) with a struct containing different fields in. I'm not saying you should do that, just that it's an option. The downside of manually working with indices is that code is more prone to off-by-one errors etc. (using iterators may be more idiomatic). That said, it's pretty clear as it stands (notwithstanding the fact that I don't actually understand the algorithm in any detail 😄).

Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
@tsmbland
tsmbland enabled auto-merge November 9, 2026 11:01
@tsmbland
tsmbland merged commit d29b0c2 into main Nov 9, 2026
@tsmbland
tsmbland deleted the investment_graph_tidy branch November 9, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants